package models

import (
	"GoTime/response"
	"time"

	"xorm.io/builder"
)

type CharStandard struct {
	Id                         int       `xorm:"not null pk autoincr comment('自增主键') INT(11)"`
	SchoolId                   int       `form:"school_id" xorm:"not null comment('学校ID') INT(11)"`
	Seq                        int       `xorm:"not null comment('收费标准编号') TINYINT(4)"`
	Subject                    string    `xorm:"comment('培训部分及方式 1:第一部分集中教学 2:第一部分网络教学 3:第四部分集中教学 4:第四部分网络教学 5:模拟器教学 6:第二部分普通教学 7:第二部分智能教学 8:第三部分普通教学 9:第三部分智能教学') ENUM('1','2','3','4','5','6','7','8','9')"`
	TrainingCarType            string    `xorm:"not null default 'C1' comment('培训车型') ENUM('A1','A2','A3','B1','B2','C1','C2','C3','C4','C5','D','E','F','M','N','P')" form:"trainingcartype"`
	TimeDays                   string    `xorm:"default '' VARCHAR(255)"`
	TrainingTime               string    `xorm:"not null default '1' comment('培训时段 1:普通时段 2:高峰时段 3:节假日时段 9:所有时段') ENUM('1','2','3','9')"`
	TrainingMode               string    `xorm:"not null default '2' comment('培训模式 1:定时培训 2:预约培训 9:其他') ENUM('1','2','9')" form:"trainingmode"`
	ChargeMode                 string    `xorm:"not null default '1' comment('收费模式 1:一次性收费 2:计时收费 9:其他') ENUM('1','2','9')" form:"chargemode"`
	PayMode                    string    `xorm:"not null default '2' comment('付费模式 1:先学后付；2:先付后学；9:其他') ENUM('1','2','9')" form:"paymode"`
	Price                      int       `xorm:"not null comment('金额 总价: chargemode=1时 小时单价: chargemode=2时') INT(11)" form:"price"`
	CalculatedPrice            float64   `xorm:"comment('计程价格') FLOAT(11,1)" form:"calculated_price"`
	TimingPrice                float64   `xorm:"comment('计时价格') FLOAT(11,1)" form:"timing_price"`
	Sub1money                  float64   `xorm:"comment('按科目收费科目一价格') FLOAT(11,1)" form:"sub1money"`
	Sub2money                  float64   `xorm:"comment('按科目收费科目2价格') FLOAT(11,1)" form:"sub2money"`
	Sub3money                  float64   `xorm:"comment('按科目收费科目3价格') FLOAT(11,1)" form:"sub3money"`
	Sub4money                  float64   `xorm:"comment('按科目收费科目4价格') FLOAT(11,1)" form:"sub4money"`
	ClassName                  string    `xorm:"not null comment('班型名称') VARCHAR(256)" form:"name"`
	UpdateTime                 time.Time `xorm:"not null default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"`
	RecordStatus               string    `xorm:"default '0' comment('备案状态 0:未备案 1:已备案') ENUM('0','1')"`
	Type                       int       `xorm:"default 0 comment('科目类型1-科目二 2-科目三') INT(1)"`
	Akaishi                    string    `xorm:"comment('上午开始') VARCHAR(255)" form:"akaishi"`
	Ajieshu                    string    `xorm:"comment('上午结束时间') VARCHAR(255)" form:"ajieshu"`
	Pkaishi                    string    `xorm:"comment('下午开始时间') VARCHAR(255)" form:"pkaishi"`
	Pjieshu                    string    `xorm:"comment('下午结束时间') VARCHAR(255)" form:"pjieshu"`
	Nkaishi                    string    `xorm:"comment('晚上开始时间') VARCHAR(255)" form:"nkaishi"`
	Njieshu                    string    `xorm:"comment('晚上结束时间') VARCHAR(255)" form:"njieshu"`
	Keshiduration              int       `xorm:"default 0 comment('课时间隔') INT(11)" form:"keshiduration"`
	Isopena                    int       `xorm:"default 1 comment('是否开启上午时段') INT(1)"`
	Isopenp                    int       `xorm:"default 1 comment('是否开启下午时段') INT(1)"`
	Isopenn                    int       `xorm:"default 1 comment('是否开启晚上时段') INT(1)"`
	Xiuxiriweek                string    `xorm:"comment('休息日设置') VARCHAR(20)" form:"xiuxiriweek"`
	Zhouqi                     int       `xorm:"comment('时段周期') INT(1)" form:"zhouqi"`
	Persnum                    int       `xorm:"comment('自定义预约人数') INT(11)" form:"persnum"`
	Isopenscoach               string    `xorm:"default '0' comment('是否开启跨班级选择教练') ENUM('0','1')"`
	Isopencoachtime            string    `xorm:"default '0' comment('是否开启教练自定义预约时段权限') ENUM('0','1')" form:"isopencoachtime"`
	Isopencoachperson          string    `xorm:"default '0' comment('是否开启教练自定义每课时预约人数权限') ENUM('0','1')"`
	Isopencoachsuject          int       `xorm:"default 0 comment('是否开启教练自定义科目权限') INT(11)"`
	Isopencomp                 int       `xorm:"default 0 comment('是否开启强制评价') INT(11)"`
	Baswage                    float32   `xorm:"comment('基本工资') FLOAT"`
	Meritpay                   float32   `xorm:"comment('绩效百分比  或 具体金额') FLOAT" form:"meritpay"`
	Favamount                  float32   `xorm:"comment('好评奖励金额') FLOAT(6,2)" form:"favamount"`
	Assamount                  float32   `xorm:"comment('差评惩罚金额') FLOAT(6,2)" form:"assamount"`
	Middamount                 float32   `xorm:"comment('中评惩罚') FLOAT(6,2)" form:"middamount"`
	DeleteStatus               string    `xorm:"VARCHAR(1)"`
	Isused                     string    `xorm:"VARCHAR(1)"`
	Appointments               string    `xorm:"comment('可以预约人数') VARCHAR(255)" form:"appointments"`
	Issetcoachtime             string    `xorm:"default '0' comment('是否允许 教练自定义上班时间  0 不允许 1允许') ENUM('0','1')" form:"issetcoachtime"`
	Timetrainingfee            int       `xorm:"comment('计时培训费用') INT(11)" form:"timetrainingfee"`
	Divisiontrainingfeeam      int       `xorm:"comment('分时段培训费上午') INT(11)" form:"divisiontrainingfeeam"`
	Divisiontrainingfeepm      int       `xorm:"comment('分时段培训费下午') INT(11)" form:"divisiontrainingfeepm"`
	Divisiontrainingfeeweek    int       `xorm:"comment('分时段培训费周六日') INT(11)" form:"divisiontrainingfeeweek"`
	Divisiontrainingfeenight   int       `xorm:"comment('分时段培训费晚上') INT(11)" form:"divisiontrainingfeenight"`
	Divisiontrainingfeeholiday int       `xorm:"comment('分时段培训费节假日') INT(11)" form:"divisiontrainingfeeholiday"`
}

func (c *CharStandard) GetSignChatStandard(standardId interface{}) (signal *CharStandard, err error) {
	_, err = engine.Table("dts_char_standard").ID(standardId).Where("isused=?", "1").Get(c)
	return c, err
}

func (c *CharStandard) GetList(filter builder.Cond, pageSize int, offset int) (list []*response.ClassListResponse, count int64, err error) {
	sess := engine.Table("dts_char_standard").Alias("dc").
		Where("dc.isused=?", "1").
		And(filter).Desc("dc.id")
	if err = sess.Limit(pageSize, offset).Find(&list); err != nil {
		return list, 0, err
	}
	_, err = engine.Table("dts_char_standard").Alias("dc").Select("count(dc.id) as count").
		Where("dc.isused=?", "1").And(filter).Get(&count)
	if err != nil {
		return list, count, err
	}
	return list, count, nil
}

func (c *CharStandard) Record(institutionCode string, charstandard *CharStandard) (signal *CharStandard, err error) {
	////备案收费标准
	//data := map[string]interface{}{
	//	"inscode":       institutionCode,                            //培训机构编号
	//	"seq":           charstandard.Id,                            //收费标准编号
	//	"vehicletype":   charstandard.TrainingCarType,               //培训车型
	//	"trainningmode": charstandard.TrainingMode,                  //培训模式
	//	"trainningtime": charstandard.TrainingTime,                  //培训时段
	//	"chargemode":    charstandard.ChargeMode,                    //收费模式
	//	"paymode":       charstandard.PayMode,                       //付费模式
	//	"price":         charstandard.Price,                         //金额
	//	"classcurr":     charstandard.ClassName,                     //班型名称
	//	"uptime":        charstandard.UpdateTime.Format("20060102"), //更新时间
	//}
	//if url, err = GetSignURL("CharStandard", data, req_type, province, c.PostForm("city")); err != nil {
	//	session.Rollback()
	//	Resp(c, 201, err.Error(), nil)
	//	return
	//}
	//var api_log models.ApiLog
	//param, err := json.Marshal(data)
	//api_log.Url = url
	//api_log.Param = string(param)
	//api_log.Method = "POST"
	//api_log.Time = time.Now()
	//if ret_map, err = APIReq(url, data, "POST"); err != nil {
	//	result, _ := json.Marshal(ret_map)
	//	api_log.Result = string(result)
	//	api_log.Add()
	//	Resp(c, 201, err.Error(), nil)
	//	return
	//}
	//result, _ := json.Marshal(ret_map)
	//api_log.Result = string(result)
	//api_log.Add()
	//fmt.Println(ret_map)
	//errorcode, _ := GetFloat64(ret_map["errorcode"])
	//if errorcode != 0 && !strings.Contains(ret_map["message"].(string), "已经备案") {
	//	session.Rollback()
	//	Resp(c, 201, adderror, nil)
	//	return
	//}
	return charstandard, nil
}
